package com.ruoyi.project.hcy.product.controller;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hcy.product.domain.img.ProImg;
import com.ruoyi.project.hcy.product.domain.product.Product;
import com.ruoyi.project.hcy.product.domain.product.ProductVo;
import com.ruoyi.project.hcy.product.service.IProImgService;
import com.ruoyi.project.hcy.product.service.IProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ProductController
 * @Author SAI
 * @Date 2020/9/18 16:05
 * @Description ProductController
 * @Version 1.0
 */
@Controller
@RequestMapping("/hcy/product/product")
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProImgService proImgService;

    private String prefix = "hcy/product/product";

    /**
     * 产品页面的转发
     * @return  页面路径
     */
    @RequiresPermissions("product:product:view")
    @GetMapping()
    public String operlog(){
        return prefix + "/product";
    }

    @RequiresPermissions("product:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Product product){
        startPage();
        List<ProductVo> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add(){
        return prefix+"/add";
    }

    @RequiresPermissions("product:product:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Product product){
        System.out.println(product);
        if ("1".equals(productService.checkProductNameUnique(product))) {
            return error("新增产品'" + product.getProductName() + "'失败，品名已存在");
        }
        return toAjax(productService.insertProduct(product));
    }

    @PostMapping("/checkProductNameUnique")
    @ResponseBody
    public String checkProductNameUnique(Product product) {
        return productService.checkProductNameUnique(product);
    }

    @GetMapping("/edit/{productId}")
    public String edit(@PathVariable("productId")Long productId, ModelMap mmap){
        Product product = productService.selectProductById(productId);
        mmap.put("product",product);
        ProImg proImg = proImgService.selectImgById(product.getImgId());
        mmap.put("imgUrl",proImg.getImgUrl());
        return prefix+"/edit";
    }

    @RequiresPermissions("product:product:edit")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Product product){
        if ("1".equals(productService.checkProductNameUnique(product))) {
            return error("修改产品'" + product.getProductName() + "'失败，品名已存在");
        }
        return toAjax(productService.updateProduct(product));
    }

    @RequiresPermissions("product:product:remove")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(productService.deleteProductByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

}
